from selenium.webdriver.chrome import options
from selenium.common.exceptions import InvalidArgumentException

from . import HSWebDriver


class ReuseRemote(HSWebDriver):
    '''
    重新远程控制已打开浏览器

    :Attributes:
     - command_executor: remote_connection.RemoteConnection object used to execute commands.
     - session_id: String ID of the browser session started and controlled by this WebDriver.

    :Usage:
        通过driver代理的url和session，连接已打开的浏览器

    :Example:
     - 例: driver = ReuseRemote(url, session)
    '''
    def __init__(self, command_executor, session_id):
        self.r_session_id = session_id
        HSWebDriver.__init__(self, command_executor=command_executor, desired_capabilities={})

    def start_session(self, capabilities, browser_profile=None):
        """
        重写start_session方法

        :Args:
         - capabilities: Dictionaty of effective capabilities of this browser session as returned by the remote server.
         - browser_profile: A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested.
        """
        if not isinstance(capabilities, dict):
            raise InvalidArgumentException("Capabilities must be a dictionary")
        if browser_profile:
            if "moz:firefoxOptions" in capabilities:
                capabilities["moz:firefoxOptions"]["profile"] = browser_profile.encoded
            else:
                capabilities.update({'firefox_profile': browser_profile.encoded})

        self.capabilities = options.Options().to_capabilities()
        self.session_id = self.r_session_id
        self.w3c = False
